<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-box">
        <h2>注册账号</h2>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              :prefix-icon="Message"
            />
          </el-form-item>
          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              placeholder="请输入手机号"
              :prefix-icon="Phone"
            />
          </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="registerForm.role" placeholder="请选择身份" style="width: 100%">
              <el-option label="用户" value="user" />
              <el-option label="管理员" value="admin" />
              <el-option label="服务人员" value="nurse" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleRegister"
              :loading="loading"
              class="register-button"
            >
              注册账号
            </el-button>
          </el-form-item>
          <div class="login-link">
            已有账号？<el-link type="primary" @click="$router.push('/login')">立即登录</el-link>
          </div>
        </el-form>
      </div>
    </div>
 
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'


const router = useRouter()
const userStore = useUserStore()
const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  email: '',
  phone: '',
  role: 'user',
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3-20个字符之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' },
  ],
  role: [{ required: true, message: '请选择身份', trigger: 'change' }],
}

const handleRegister = async () => {
  if (!registerFormRef.value) return

  try {
    await registerFormRef.value.validate()
    loading.value = true

    await userStore.register(registerForm)
    ElMessage.success('注册成功，请登录')
    router.push('/login')
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '注册失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.register-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #e6f3ff 0%, #f0f9ff 100%);
  padding: clamp(10px, 3vw, 20px);
}

.register-box {
  width: 100%;
  max-width: min(90vw, 400px);
  padding: clamp(20px, 5vw, 40px);
  background: white;
  border-radius: clamp(8px, 2vw, 12px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: var(--el-color-primary);
  margin-bottom: clamp(20px, 4vw, 30px);
  font-size: clamp(20px, 4vw, 24px);
  font-weight: 600;
}

.register-button {
  width: 100%;
  padding: clamp(10px, 2vw, 12px) 0;
  font-size: clamp(14px, 3vw, 16px);
  font-weight: 500;
}

.login-link {
  text-align: center;
  margin-top: clamp(12px, 3vw, 16px);
  font-size: clamp(13px, 2.5vw, 14px);
  color: var(--el-text-color-regular);
}

:deep(.el-input__wrapper) {
  padding: 0 clamp(8px, 2vw, 11px);
  box-shadow: 0 0 0 1px var(--el-border-color) inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}

:deep(.el-input__inner) {
  height: clamp(36px, 8vw, 42px);
  font-size: clamp(14px, 3vw, 16px);
}

:deep(.el-form-item__error) {
  font-size: clamp(12px, 2.5vw, 13px);
  padding-top: 4px;
}

@media (max-width: 480px) {
  .register-box {
    margin: 10px;
    padding: 20px;
  }

  :deep(.el-form-item) {
    margin-bottom: 15px;
  }

  :deep(.el-input__inner) {
    height: 40px;
  }
}

@media (prefers-color-scheme: dark) {
  .register-container {
    background: linear-gradient(135deg, #1a1a1a 0%, #2c2c2c 100%);
  }

  .register-box {
    background: #2c2c2c;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  }

  h2 {
    color: var(--el-color-primary);
  }

  .login-link {
    color: var(--el-text-color-secondary);
  }
}
</style>
